Skip to content

feat(fspy_elf_loader): add ELF validation, PF_X metadata, and dynamic stack sizing - #1027

Open
wan9chi wants to merge 1 commit into
fspy-linux/1-nostd-primitivesfrom
fspy-linux/2-elf-loader-validation-stack
Open

wan9chi wants to merge 1 commit into
fspy-linux/1-nostd-primitivesfrom
fspy-linux/2-elf-loader-validation-stack

Conversation

@wan9chi

@wan9chi wan9chi commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Motivation

Before fspy_elf_loader can host a SIGSYS syscall-virtualization runtime for arbitrary Linux executables, it needs fallible pre-exec ELF validation (so invalid executables return ENOEXEC instead of panicking inside the launcher), fixed high-address link-arg propagation for IP allowlisting, RLIMIT_STACK-aware stack sizing, and /proc/self/comm initialization matching kernel execve.

Changes

  • Export fixed-address --no-pie --image-base=... link args from fspy_elf_loader/build.rs via links metadata and consume them in fspy_elf_loader_test_launcher/build.rs.
  • Add validate_exec, fallible ELF metadata mapping (Mapping::try_file), PT_GNU_STACK PF_X rejection, ET_DYN high-p_vaddr load-bias fixes, and stricter segment validation with unit tests in image/tests.rs.
  • Size the target process stack dynamically from RLIMIT_STACK via prlimit64 (clamped to [8 MiB, 64 MiB]) with unit tests in stack.rs, and update /proc/self/comm via PR_SET_NAME.

Stack created with GitHub Stacks CLI • Give Feedback 💬


Note

High Risk
Touches ELF mapping/load-bias logic, mmap validation, and pre-exec process setup (stack limits, comm); bugs could break loading or allow unsafe images.

Overview
Adds validate_exec so callers can reject bad ELF executables (and nested PT_INTERP rules) with Result instead of panics, without mapping code or closing the fd. ELF parsing moves to BorrowedFd, splits segment validation (ValidatedLayout) from mapping, hardens checks (including executable PT_GNU_STACK), and fixes ET_DYN load bias for high p_vaddr via wrapping arithmetic.

exec_elf now runs validation before map, sets /proc/self/comm with PR_SET_NAME, and sizes the initial stack from RLIMIT_STACK via prlimit64 (clamped 8–64 MiB). Fixed --no-pie --image-base=... link args are emitted from fspy_elf_loader/build.rs through Cargo links metadata and reused by the test launcher.

Reviewed by Cursor Bugbot for commit 28f84bd. Bugbot is set up for automated code reviews on this repo. Configure here.

@wan9chi
wan9chi added this pull request to stack #1030 November 27, 2026 00:33
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

fspy benchmark

linux

dynamic/launch             change  -0.42%  [ -4.19% ..  +4.09%]  overhead   +57.03%
dynamic/access             change  -0.39%  [-12.47% ..  +4.25%]  overhead   +13.42%
dynamic/access-relative    change  -0.81%  [-17.44% ..  +1.76%]  overhead   +73.45%
dynamic/access-contended   change  +3.17%  [ -6.76% .. +14.85%]  overhead   +13.55%
static/launch              change  +0.32%  [ -3.58% ..  +5.39%]  overhead  +196.71%
static/access              change  -0.90%  [-10.37% ..  +8.87%]  overhead +1254.86%
static/access-relative     change  -1.24%  [ -8.68% ..  +3.97%]  overhead +1917.30%
static/access-contended    change  +0.82%  [ -1.57% .. +13.59%]  overhead +1570.50%

macos

dynamic/launch             change  -0.03%  [ -2.41% ..  +2.61%]  overhead  +225.99%
dynamic/access             change  +0.66%  [ -1.50% ..  +1.94%]  overhead    +4.80%
dynamic/access-relative    change  +0.36%  [ -2.82% ..  +4.32%]  overhead  +245.64%
dynamic/access-contended   change  -0.09%  [ -3.02% ..  +2.47%]  overhead    +3.34%

windows

dynamic/launch             change  +0.17%  [ -3.73% ..  +5.29%]  overhead   +27.51%
dynamic/access             change  +0.18%  [ -0.90% ..  +2.22%]  overhead    +0.92%
dynamic/access-relative    change  -0.18%  [ -1.91% ..  +1.79%]  overhead    +1.26%
dynamic/access-contended   change  -0.53%  [ -7.80% ..  +3.20%]  overhead    +2.28%

… stack sizing

Harden ELF image validation and stack setup ahead of SIGSYS syscall
virtualization:

- Export fixed-address `--no-pie --image-base=...` link args from
  `fspy_elf_loader/build.rs` and consume them in the test launcher.
- Add `validate_exec`, fallible ELF metadata mapping (`Mapping::try_file`),
  `PT_GNU_STACK` `PF_X` rejection, and stricter segment validation with
  unit tests in `image/tests.rs`.
- Size the target process stack dynamically from `RLIMIT_STACK` (clamped
  to `[8 MiB, 64 MiB]`) with unit tests in `stack.rs`, and update
  `/proc/self/comm` via `PR_SET_NAME`.

Co-authored-by: Gemini 4 Neon <noreply@google.com>
@wan9chi
wan9chi force-pushed the fspy-linux/2-elf-loader-validation-stack branch from 3a359d1 to 28f84bd Compare November 27, 2026 00:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant